angular
    .module("Ajani", ["ui", "ngSanitize", "Ajani.filters", "Ajani.services", "Ajani.directives", "Ajani.controllers"])
    .config(["$routeProvider", "$locationProvider", ($routeProvider, $locationProvider) ->
        $routeProvider.when "/", 
            templateUrl: "/partials/home.html"
            controller: 'HomeController'
            resolve:
                'ItemsHome': ['$q', '$route', '$timeout', 'Items', ($q, $route, $timeout, Items) ->
                    deferred = $q.defer()
                    success = (result) ->
                        deferred.resolve result

                    Items.all({q: "home"}, success)
                    # $timeout () ->
                    #     Items.all({q: "home"}, success)
                    # , 1000

                    deferred.promise
                ]

        $routeProvider.when "/shops", 
            templateUrl: "/partials/shops.html"
            controller: 'ShopsController'
            resolve:
                'ItemsShop': ['$q', '$route', '$timeout', 'Items', ($q, $route, $timeout, Items) ->
                    deferred = $q.defer()
                    success = (result) ->
                        deferred.resolve result

                    # $timeout () ->
                    #     Items.all({}, success)
                    # , 1000
                    Items.all({}, success)

                    deferred.promise
                ]

        $routeProvider.when "/shops/:cat", 
            templateUrl: "/partials/shops.html"
            controller: 'ShopsController'
            resolve:
                'ItemsShop': ['$q', '$route', '$timeout', 'Items', ($q, $route, $timeout, Items) ->
                    cat = $route.current.params.cat
                    deferred = $q.defer()
                    success = (result) ->
                        deferred.resolve result

                    # $timeout () ->
                    #     Items.all({cat:cat}, success)
                    # , 1000
                    Items.all({cat:cat}, success)

                    deferred.promise
                ]

        $routeProvider.when "/register", 
            templateUrl: "/partials/register.html"

        $routeProvider.when "/signin", 
            templateUrl: "/partials/lsignin.html"
            controller: 'UserSigninController'

        $routeProvider.when "/signout", 
            templateUrl: "/partials/lsignout.html"
            controller: 'UserSignoutController'

        $routeProvider.when "/post-new-item", 
            templateUrl: "/partials/item/new.html"
            controller: "ItemNewController"
            resolve: 
                'UserProfile': ['$q', '$route', '$timeout', 'User', ($q, $route, $timeout, User) ->
                    deferred = $q.defer()
                    success = (result) ->
                        if result.status == "success"
                            deferred.resolve result
                        else
                            deferred.reject result.message

                    # $timeout () ->
                    #     User.me({q: "detail"}, success)
                    # , 1000
                    User.me({q: "detail"}, success)

                    deferred.promise
                ]

        $routeProvider.when "/user/:username", 
            templateUrl: "/partials/profile.html",
            controller: 'UserAreaController',
            resolve: 
                'UserProfile': ['$q', '$route', '$timeout', 'User', ($q, $route, $timeout, User) ->
                    deferred = $q.defer()
                    success = (result) ->
                        if result.status == "error"
                            deferred.reject result.message
                        else
                            deferred.resolve result

                    # $timeout () ->
                    #     User.me({q: "detail"}, success)
                    # , 1000
                    User.me({q: "detail"}, success)

                    deferred.promise
                ]
                'UserResource': () ->

        $routeProvider.when "/user/:username/:res", 
            templateUrl: "/partials/profile.html"
            controller: 'UserAreaController'
            resolve:
                'UserProfile': ['$q', '$route', '$timeout', 'User', ($q, $route, $timeout, User) ->
                    deferred = $q.defer()
                    success = (result) ->
                        if result.status == "error"
                            deferred.reject result.message
                        else
                            deferred.resolve result

                    # $timeout () ->
                    #     User.me({q: "detail"}, success)
                    # , 1000
                    User.me({q: "detail"}, success)

                    deferred.promise
                ]
                'UserResource': ['$q', '$route', '$timeout', 'Message', 'Items', ($q, $route, $timeout, Message, Items) ->
                    deferred = $q.defer()
                    success = (result) ->
                        if result.status == "error"
                            deferred.reject result.message
                        else
                            deferred.resolve result

                    # $timeout () ->
                    #     if $route.current.params.res == "inbox" or $route.current.params.res == "outbox"
                    #         Message.list({q: $route.current.params.res, ct: "1"}, success)
                    #     else if $route.current.params.res == "for-sell"
                    #         Items.all({u: "user"}, success)
                    # , 1000
                    if $route.current.params.res == "inbox" or $route.current.params.res == "outbox"
                        Message.list({q: $route.current.params.res, ct: "1"}, success)
                    else if $route.current.params.res == "for-sell"
                        Items.all({u: "user"}, success)

                    deferred.promise
                ]

        $routeProvider.when "/admin", 
            templateUrl: "/partials/admin.html",
            controller: 'AdminAreaController',
            resolve: 
                'UserProfile': ['$q', '$route', '$timeout', 'User', ($q, $route, $timeout, User) ->
                    deferred = $q.defer()
                    success = (result) ->
                        if result.status == "error"
                            deferred.reject result.message
                        else
                            if not result.admin
                                deferred.reject "Unauthorized Access"
                            else
                                deferred.resolve result

                    # $timeout () ->
                    #     User.me({q: "detail"}, success)
                    # , 1000
                    User.me({q: "detail"}, success)

                    deferred.promise
                ]
                'AdminRes': () ->

        $routeProvider.when "/admin/:pat", 
            templateUrl: "/partials/admin.html",
            controller: 'AdminAreaController',
            resolve: 
                'UserProfile': ['$q', '$route', '$timeout', 'User', ($q, $route, $timeout, User) ->
                    deferred = $q.defer()
                    success = (result) ->
                        if result.status == "error"
                            deferred.reject result.message
                        else
                            if not result.admin
                                deferred.reject "Unauthorized Access"
                            else
                                deferred.resolve result

                    # $timeout () ->
                    #     User.me({q: "detail"}, success)
                    # , 1000
                    User.me({q: "detail"}, success)

                    deferred.promise
                ]
                'AdminRes': () ->

        $routeProvider.when "/admin/:pat/:sub", 
            templateUrl: "/partials/admin.html",
            controller: 'AdminAreaController',
            resolve: 
                'UserProfile': ['$q', '$route', '$timeout', 'User', 'Admin', ($q, $route, $timeout, User, Admin) ->
                    deferred = $q.defer()

                    auth_success = (result) ->
                        if result.status == "error"
                            deferred.reject result.message
                        else
                            if not result.admin
                                deferred.reject "Unauthorized Access"
                            else
                                deferred.resolve result

                    # $timeout () ->
                    #     User.me({q: "detail"}, auth_success)
                    # , 1000
                    User.me({q: "detail"}, auth_success)

                    deferred.promise
                ]
                'AdminRes': ['$q', '$route', '$timeout', 'Admin', ($q, $route, $timeout, Admin) ->
                    deferred = $q.defer()

                    success = (result) ->
                        if result.status == "error"
                            deferred.reject result.message
                        else
                            deferred.resolve result

                    # $timeout () ->
                    #     if $route.current.params.pat == "resources"
                    #         if $route.current.params.sub == "users"
                    #             Admin.users {
                    #                 p:if $route.current.params.p then $route.current.params.p else ''
                    #                 c:if $route.current.params.c then $route.current.params.c else ''
                    #             }, success
                    #         else if $route.current.params.sub == "items"
                    #             Admin.items {
                    #                 p:if $route.current.params.p then $route.current.params.p else ''
                    #                 c:if $route.current.params.c then $route.current.params.c else ''
                    #             }, success
                    # , 1000
                    if $route.current.params.pat == "resources"
                        if $route.current.params.sub == "users"
                            Admin.users {
                                p:if $route.current.params.p then $route.current.params.p else ''
                                c:if $route.current.params.c then $route.current.params.c else ''
                            }, success
                        else if $route.current.params.sub == "items"
                            Admin.items {
                                p:if $route.current.params.p then $route.current.params.p else ''
                                c:if $route.current.params.c then $route.current.params.c else ''
                            }, success

                    deferred.promise
                ]

        $locationProvider.html5Mode(true)
    ])
    # .config(['$httpProvider', ($httpProvider) ->
    #     interceptor = ['$q', '$injector', ($q, $injector) ->
    #         $http = undefined
    #         success = (response) ->
    #             response

    #         error = (response) ->
    #             if response.status is 401
    #                 $http = $http || $injector.get('$http');
    #                 deferred = $q.defer()
    #                 $http(response.config).then (data) ->
    #                     deferred.resolve data
    #                 deferred.promise
    #             else
    #                 $q.reject 'error'

    #         ret = (promise) ->
    #             promise.then success, error

    #         ret
    #     ]

    #     $httpProvider.responseInterceptors.push(interceptor)
    # ])
    .value("ui.config",
        currency:
            symbol: 'Rp'
    )